package logic

import (
	"context"
	"net/http"
	"zg6/zg6_month/goodssrv/goodssrv"
	"zg6/zg6_month/ordersrv/ordersrv"

	"zg6/zg6_month/client/internal/svc"
	"zg6/zg6_month/client/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type CreateOrderLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewCreateOrderLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateOrderLogic {
	return &CreateOrderLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *CreateOrderLogic) CreateOrder(req *types.CreateOrderReq) (resp *types.Response, err error) {
	// todo: add your logic here and delete this line
	var list = []*ordersrv.Goods{
		&ordersrv.Goods{
			Id:  1,
			Num: 10,
		},
		&ordersrv.Goods{
			Id:  2,
			Num: 20,
		},
	}
	//调用库存判断库存是否充足
	_, err = l.svcCtx.GoodsSrv.SearchGoods(l.ctx, &goodssrv.SearchGoodsReq{
		Title: "1",
		Page:  1,
	})
	if err != nil {
		return nil, err
	}
	//生成订单
	res, err := l.svcCtx.OrderSrv.CreateOrder(l.ctx, &ordersrv.CreateOrderReq{
		Goods:      list,
		TotalPrice: float32(req.TotalPrice),
	})
	if err != nil {
		return nil, err
	}
	return &types.Response{
		Code:    http.StatusOK,
		Message: "success",
		Data: map[string]interface{}{
			"data": res.OrderSn,
		},
	}, nil
}
